package com.lz.service.lg.impl;

import com.lz.entity.*;
import com.lz.repository.lg.MortgageSheetDaolg;
import com.lz.repository.lg.OrderSheetDao;
import com.lz.repository.lg.VO.MortgageVO;
import com.lz.repository.lg.impl.OrderSheetDaoImpl;
import com.lz.service.lg.MortgageSheetServicelg;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
@Service
public class MortgageSheetServiceImpllg implements MortgageSheetServicelg {
	@Autowired
	private SessionFactory sessionFactory;
	@Autowired
	private MortgageSheetDaolg mortgageSheetDao;
	
	@Override
	public void addMortgage(MortgageVO mortgageVO) {
		Session session = sessionFactory.openSession();
		String orderId = mortgageVO.getOrderId();
		//根据订单单号查询到对应的订单
		OrderSheetDao orderSheetDao = new OrderSheetDaoImpl();
		String hql = "from OrderSheet os where os.orderId = "+String.valueOf(orderId);
		OrderSheet orderSheet = orderSheetDao.findOrderSheet(session, hql);
		Car car = orderSheet.getCar();
		Employee employee = orderSheet.getEmployee();
		session.save(employee);
		session.save(car);
		//根据订单得到对应的客户信息
		Customer customer = orderSheet.getCustomer();
		session.save(customer);
		//保存金融按揭机构信息
		String hql2 = "from Bank b where b.bankName = :bankName";
		Query query2 = session.createQuery(hql2);
		query2.setString("bankName",mortgageVO.getBankName());
		Object o = query2.uniqueResult();
		Bank bank = new Bank();
		bank.setBankName(mortgageVO.getBankName());
		bank.setBankType(mortgageVO.getBankType());
		if(o != null){
			bank = (Bank) o;
		}
		session.save(bank);
		//新建按揭单
		MortgageSheet mortgageSheet = new MortgageSheet();
		session.save(mortgageSheet);
		//将银行信息保存到按揭单中
		mortgageSheet.setBank(bank);
		mortgageSheet.setCar(car);
		mortgageSheet.setEmployee(employee);
		mortgageSheet.setOrderId(Integer.parseInt(orderId));
		mortgageSheet.setServiceCharge(Double.parseDouble(mortgageVO.getServiceCharge()));
		mortgageSheet.setMortgageSheetDate(new Date());
		mortgageSheet.setMortgageSheetState("已保存");
		mortgageSheet.setCustomer(customer);
		mortgageSheet.setLoanTime(Integer.parseInt(mortgageVO.getLoanTime()));
		//得到汽车价格
		Double carPrice = car.getPrice();
		String downPayments = mortgageVO.getDownPayments();
		double v = (carPrice-Double.parseDouble(downPayments))*(Double.parseDouble(mortgageVO.getInterestRate()) + 1);
		mortgageSheet.setLoanMoney(v);
		mortgageSheet.setDownPayments(Double.parseDouble(downPayments));
		mortgageSheet.setInterestRate(Double.parseDouble(mortgageVO.getInterestRate()));
		CreditCard creditCard = new CreditCard();
		session.save(creditCard);
		creditCard.setCustomer(customer);
		creditCard.setCreditCardId(mortgageVO.getCreditCardId());
		creditCard.setQuota(Double.parseDouble(mortgageVO.getQuota()));
		creditCard.setUsedQuota(Double.parseDouble(mortgageVO.getUsedQuota()));
		Unit unit = new Unit();
		session.save(unit);
		customer.setUnit(unit);
		unit.setUnitName(mortgageVO.getUnitName());
		unit.setPayMoney(mortgageVO.getPayMethod());
		Credit credit = new Credit();
		session.save(credit);
		customer.setCredit(credit);
		credit.setOverdueInfo(mortgageVO.getOverdueInfo());
		credit.setApplyTimes(Integer.parseInt(mortgageVO.getApplyTimes()));
		credit.setPassTimes(Integer.parseInt(mortgageVO.getPassTimes()));
		mortgageSheet.setMortgageMsg(mortgageVO.getMortgageMsg());
		session.flush();
	}

	//得到总条数
	@Override
	public Integer mortgageRows(Map map) {
		Session session = sessionFactory.openSession();
		StringBuffer hql = new StringBuffer("from MortgageSheet ms where 1=1 ");
		Iterator iterator = map.entrySet().iterator();
		while(iterator.hasNext()){
			Map.Entry<String, String> next = (Map.Entry<String, String>)iterator.next();
			String key = next.getKey();
			String value = next.getValue();
			if(!"".equals(value) && value!=null){
				hql.append("and ms.");
				hql.append(key);
				hql.append(" like '%");
				hql.append(value);
				hql.append("%' ");
			}
		}
		Integer rows = mortgageSheetDao.rows(session, hql.toString());
		return rows;
	}

	//条件查询按揭单结果集
	@Override
	public List<MortgageVO> mortgageList(Map map, Integer pageNo, Integer pageSize) {
		Session session = sessionFactory.getCurrentSession();
		StringBuffer hql = new StringBuffer("from MortgageSheet ms where 1=1 ");
		Iterator iterator = map.entrySet().iterator();
		while(iterator.hasNext()){
			Map.Entry<String, String> next = (Map.Entry<String, String>)iterator.next();
			String key = next.getKey();
			String value = next.getValue();
			if(!"".equals(value) && value!=null){
				hql.append("and ms.");
				hql.append(key);
				hql.append(" like '%");
				hql.append(value);
				hql.append("%' ");
			}
		}
		List<MortgageSheet> mortgageList = mortgageSheetDao.selectMortgageList(session, hql.toString(), pageNo, pageSize);
		ArrayList<MortgageVO> arrayList = new ArrayList<>();
		for (MortgageSheet mortgageSheet : mortgageList) {
			MortgageVO mortgageVO = new MortgageVO();
			mortgageVO.setMortgageSheetNo(String.valueOf(mortgageSheet.getMortgageSheetNo()));
			mortgageVO.setCustomerName(mortgageSheet.getCustomer().getCustomerName());
			mortgageVO.setCustomerTel(mortgageSheet.getCustomer().getCustomerTel());
			mortgageVO.setBankName(mortgageSheet.getBank().getBankName());
			mortgageVO.setMortgageSheetState(mortgageSheet.getMortgageSheetState());
			Date mortgageSheetDate = mortgageSheet.getMortgageSheetDate();
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String format = dateFormat.format(mortgageSheetDate);
			mortgageVO.setMortgageSheetDate(format);
			mortgageVO.setMortgageSheetState(mortgageSheet.getMortgageSheetState());
			arrayList.add(mortgageVO);
		}
		return arrayList;
	}

	@Override
	public MortgageVO selectByPK(Integer MortgageNo) {
		Session session = sessionFactory.getCurrentSession();
		MortgageSheet mortgageSheet = mortgageSheetDao.selectByPK(session, MortgageNo);
		Customer customer = mortgageSheet.getCustomer();
		Unit unit = customer.getUnit();
		Car car = mortgageSheet.getCar();
		session.save(car);
		session.save(unit);
		Bank bank = mortgageSheet.getBank();
		MortgageVO mortgageVO = new MortgageVO();
		mortgageVO.setBank(bank);
		mortgageVO.setCustomer(customer);
		mortgageVO.setMortgageSheet(mortgageSheet);
		mortgageVO.setCustomerName(customer.getCustomerName());
		mortgageVO.setMortgageSheetState(mortgageSheet.getMortgageSheetState());
		mortgageVO.setMortgageSheetNo(String.valueOf(mortgageSheet.getMortgageSheetNo()));
		Date mortgageSheetDate = mortgageSheet.getMortgageSheetDate();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
		String format = simpleDateFormat.format(mortgageSheetDate);
		mortgageVO.setMortgageSheetDate(format);
		Integer time = mortgageSheet.getLoanTime();
		mortgageVO.setLoanTime(String.valueOf(time));
		mortgageVO.setLoanMoney(String.valueOf(mortgageSheet.getLoanMoney()));
		mortgageVO.setDownPayments(String.valueOf(mortgageSheet.getDownPayments()));
		mortgageVO.setInterestRate(String.valueOf(mortgageSheet.getInterestRate()));
		mortgageVO.setCustomerName(customer.getCustomerName());
		mortgageVO.setCustomerCardId(customer.getCustomerCardId());
		mortgageVO.setCustomerTel(customer.getCustomerTel());
		mortgageVO.setCustomerAddress(customer.getCustomerAddress());
		mortgageVO.setUnitName(unit.getUnitName());
		mortgageVO.setPayMethod(unit.getPayMoney());
		mortgageVO.setBrand(car.getBrand());
		mortgageVO.setSeries(car.getSeries());
		mortgageVO.setModel(car.getModel());
		mortgageVO.setSeries(car.getSeries());
		mortgageVO.setColor(car.getColor());
		mortgageVO.setPrice(String.valueOf(car.getPrice()));
		mortgageVO.setConfigure(car.getConfigure());
		mortgageVO.setVin(car.getVin());
		mortgageVO.setBankName(bank.getBankName());
		mortgageVO.setBankType(bank.getBankType());
		return mortgageVO;
	}

	@Override
	public Integer selectMaxId() {
		Session session = sessionFactory.getCurrentSession();
		String hql = "from MortgageSheet ms order by ms.mortgageSheetNo";
		Query query = session.createQuery(hql);
		List list = query.list();
		if(list == null){
			return 1;
		}
		MortgageSheet o = (MortgageSheet)list.get(list.size() - 1);
		return o.getMortgageSheetNo();
	}

	@Override
	public void changeMortgageSheet(MortgageVO mortgageVO) {
		Session session = sessionFactory.getCurrentSession();
		String mortgageSheetNo = mortgageVO.getMortgageSheetNo();
		MortgageSheet mortgageSheet = (MortgageSheet)session.get(MortgageSheet.class, Integer.parseInt(mortgageSheetNo));
		String loanTime = mortgageVO.getLoanTime();
		mortgageSheet.setLoanTime(Integer.parseInt(loanTime));
		mortgageSheet.setLoanMoney(Double.parseDouble(mortgageVO.getLoanMoney()));
		mortgageSheet.setDownPayments(Double.parseDouble(mortgageVO.getDownPayments()));
		String hql = "from Bank b where b.bankName = :bankName";
		Query query = session.createQuery(hql);
		query.setString("bankName",mortgageVO.getBankName());
		Bank bank = (Bank)query.uniqueResult();
		mortgageSheet.setBank(bank);
		session.flush();
	}

	@Override
	public MortgageSheet findByPK(Integer mortgageSheetNo,MortgageVO mortgageVO) {
		Session session = sessionFactory.getCurrentSession();
		MortgageSheet mortgageSheet = mortgageSheetDao.selectByPK(session, mortgageSheetNo);
		session.save(mortgageSheet);
		mortgageSheet.setLoanTime(Integer.parseInt(mortgageVO.getLoanTime()));
		Bank bank = mortgageSheet.getBank();
		session.save(bank);
		mortgageSheet.setLoanMoney(Double.parseDouble(mortgageVO.getLoanMoney()));
		mortgageSheet.setDownPayments(Double.parseDouble(mortgageVO.getDownPayments()));
		bank.setBankName(mortgageVO.getBankName());
		bank.setBankType(mortgageVO.getBankType());
		session.flush();
		return mortgageSheet;
	}

	@Override
	public void byPK(Integer mortgageSheetNo) {
		Session session = sessionFactory.getCurrentSession();
		MortgageSheet mortgageSheet = mortgageSheetDao.selectByPK(session, mortgageSheetNo);
		session.delete(mortgageSheet);
		session.flush();
	}

	@Override
	public void submitMortgage(Integer mortgageSheetNo) {
		Session session = sessionFactory.getCurrentSession();
		MortgageSheet mortgageSheet = mortgageSheetDao.selectByPK(session, mortgageSheetNo);
		mortgageSheet.setMortgageSheetState("已提交");
		session.update(mortgageSheet);
		session.flush();
	}

}
